<?php
/**
 * Created by 老吴.
 * UserMsg:砥砺前行，扬帆起航
 * email:cwwx0128@qq.com
 * QQ:1113249273
 * QQ群:925283872
 * 微信:cww0128
 * Date: 2021/4/29
 * Time: 17:14
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;
use think\Loader;

class Cusaddress extends Api
{
    protected $noNeedLogin = ['init', 'notice'];
    protected $noNeedRight = '*';

    /**
     * 新增地址
     */
    public function add_address()
    {
        $param = $this->request->param();
        $validate = Loader::validate('Cusaddress');
        if (!$validate->scene('add_address')->check($param)) {
            $this->error($validate->getError());
        }
        $result = false;
        /*开启事务*/
        Db::startTrans();
        try {


            /*更新-是否默认*/
            if ($param['switch'] == '1') {
                $address = new \app\admin\model\Cusaddress();
                $address->save([
                    'switch' => 0
                ], ['user_id' => $this->auth->id]);
            }

            /*新增*/
            $param['user_id'] = $this->auth->id;
            $address2 = new \app\admin\model\Cusaddress();
            $address2->data($param);
            $address2->save();
            Db::commit();
            $result = true;
        } catch (Exception $e) {
            Db::rollback();
        }

        if ($result) {
            $this->success('新增成功');
        } else {
            $this->error('新增失败');
        }
    }

    /**
     * 编辑地址
     */
    public function edit_address()
    {
        $param = $this->request->param();
        $validate = Loader::validate('Cusaddress');
        if (!$validate->scene('edit_address')->check($param)) {
            $this->error($validate->getError());
        }
        $result = false;
        /*开启事务*/
        Db::startTrans();
        try {

            /*是否默认*/
            if ($param['switch'] == '1') {
                $address = new \app\admin\model\Cusaddress();
                $address->save([
                    'switch' => 0
                ], ['user_id' => $this->auth->id]);
            }


            $edit_address = new \app\admin\model\Cusaddress();
            $id = $param['id'];
            unset($param['id']);
            $edit_address->save($param, ['id' => $id]);
            Db::commit();
            $result = true;
        } catch (Exception $e) {
            Db::rollback();
        }

        if ($result) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }

    /**
     * 地址列表管理
     */
    public function list_address()
    {
        $page = $this->request->param('page', 1);
        $limit = $this->request->param('limit', 10);
        $list = \app\admin\model\Cusaddress::where('user_id', $this->auth->id)->order('updatetime desc')->paginate($limit, false, ['page' => $page]);
        $this->success('', $list);
    }

    /**
     * 删除地址
     */
    public function del_address()
    {
        $id = $this->request->param('id', 0);
        if (!$id) $this->error('ID不能为空');
        $ret = \app\admin\model\Cusaddress::destroy($id);
        if ($ret) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}